// ReactorEx5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "SOCK_WrapperFacade.h"
#include "AlarmEvent.h"
#include "Alarm.h"
#include "LogEvent.h"
#include "Log.h"
#include "PatientEvent.h"
#include "Patient.h"
#include "Reactor.h"
#include "Acceptor.h"
#include "Connector.h"
#include "LF_EventHandler.h"
#include "ThreadPool.h"
#include "StopWatch.h"

//==========================================================================================
#define _SERVER					1	// If enabled then compile server
#define _CLIENT  				0	// else if enabled then compile client
									// else compile client tester

// Server configuration
#define MAX_THREADS				10	// Threads in pool for SERVER
#define DEFAULT_PORT_LOG		27015
#define DEFAULT_PORT_PATIENT	27016
#define DEFAULT_PORT_ALARM		27017
#define THREAD_TIMEOUT			10  // Timeout in us for server to wait on select

// Client configuration
#define CLIENT_TYPE				3	// 1 = Patient, 2 = Log, 3 = Alarm
#define ASYNC_CLIENT_CONNECT	true

Thread_Manager* Thread_Manager::threadManager = NULL;

#include "Server.h"
#include "Client.h"

//==========================================================================================
// MAIN program that implements either SERVER or CLIENT
//==========================================================================================
int _tmain(int argc, _TCHAR* argv[])
{
	unsigned long address = 0x7F000001;

	// Convert server address to hexadecimal base.
	if (argc > 1) {
		printf("Server set as %s\n", argv[1]);
		address = ntohl(inet_addr((const char*)argv[1]));
	} else
		printf("Server set as localhost\n");

	// On windows start socket 2.2 system
	WSADATA wsa_data;
	WORD version_requested = MAKEWORD(2, 2);
	int error = WSAStartup(version_requested, &wsa_data);
	if (error != 0) return -1;

#if _SERVER == 1
	Server server(address);
	server.run();
#elif _CLIENT == 1
	Client client(address);
	client.run(CLIENT_TYPE, ASYNC_CLIENT_CONNECT);
#else
	Client client(address);
	client.runLoopTest(CLIENT_TYPE);
#endif

	return 0;
}

